package com.hckjsec.cloud.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.hckjsec.common.utils.SecurityUtils;
import com.hckjsec.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.cloud.domain.AppletsBusinessVerification;
import com.hckjsec.cloud.service.IAppletsBusinessVerificationService;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.common.core.page.TableDataInfo;

/**
 * 身份核验Controller
 * 
 * @author cyt
 * @date 2021-09-07
 */
@RestController
@RequestMapping("/system/verification")
public class AppletsBusinessVerificationController extends BaseController
{
    @Autowired
    private IAppletsBusinessVerificationService appletsBusinessVerificationService;

    /**
     * 查询身份核验列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AppletsBusinessVerification appletsBusinessVerification)
    {
        startPage();
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            appletsBusinessVerification.setDeptId(SecurityUtils.getDeptId());
        }
        appletsBusinessVerification.setVertype("verification");
        List<AppletsBusinessVerification> list = appletsBusinessVerificationService.selectAppletsBusinessVerificationList(appletsBusinessVerification);
        return getDataTable(list);
    }

    /**
     * 导出身份核验列表
     */
    @Log(title = "身份核验", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AppletsBusinessVerification appletsBusinessVerification)
    {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            appletsBusinessVerification.setDeptId(SecurityUtils.getDeptId());
        }
        appletsBusinessVerification.setVertype("verification");
        List<AppletsBusinessVerification> list = appletsBusinessVerificationService.selectAppletsBusinessVerificationList(appletsBusinessVerification);
        ExcelUtil<AppletsBusinessVerification> util = new ExcelUtil<AppletsBusinessVerification>(AppletsBusinessVerification.class);
        return util.exportExcel(list, "verification");
    }

    /**
     * 获取身份核验详细信息
     */
    @GetMapping(value = "/{biztoken}")
    public AjaxResult getInfo(@PathVariable("biztoken") String biztoken)
    {
        List<String> imgList = new ArrayList<>();
        AppletsBusinessVerification appletsBusinessVerification = appletsBusinessVerificationService.selectAppletsBusinessVerificationById(biztoken);
        imgList.add(appletsBusinessVerification.getOcrfront());
        imgList.add(appletsBusinessVerification.getOcrback());
        String bestframe = StringUtils.isHcNull(appletsBusinessVerification.getBestframe(),"");
        if (!"".equals(bestframe)){
            List<String> bestframeList = Arrays.asList(bestframe.split(";"));
            for (String img : bestframeList){
                imgList.add(img);
            }
        }
        appletsBusinessVerification.setImgList(imgList);
        return AjaxResult.success(appletsBusinessVerification);
    }
}
